package com.guoyansoft.model.service.impl;

import com.guoyansoft.config.PasswordEncryption;
import com.guoyansoft.config.R;
import com.guoyansoft.model.dao.CourierAndPendingOrdersDao;
import com.guoyansoft.model.dao.CourierDao;
import com.guoyansoft.model.pojo.Courier;
import com.guoyansoft.model.pojo.CourierAndPendingOrders;
import com.guoyansoft.model.pojo.Yardman;
import com.guoyansoft.model.service.CourierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author ：zyx
 * @date ：Created in 2023-04-26-17:00
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class CourierServiceImpl implements CourierService {
    @Autowired
    private CourierDao courierDao;
    @Autowired
    private CourierAndPendingOrdersDao courierAndPendingOrdersDao;

    @Override
    public R save(Courier courier) {
        String newPass = PasswordEncryption.getCiphertext(courier.getPass());
        courier.setPass(newPass);
        int row = courierDao.insert(courier);
        return row > 0 ? R.suc() : R.fail();
    }

    @Override
    public R drop(Integer[] id) {
        int row = courierDao.delete(id);
        return row > 0 ? R.suc() : R.fail();
    }

    @Override
    public R modify(Courier courier) {
        String newPass = PasswordEncryption.getCiphertext(courier.getPass());
        courier.setPass(newPass);
        int row = courierDao.update(courier);
        return row > 0 ? R.suc() : R.fail();
    }

    @Override
    public R queryByPage(Map<String, Object> map) {
        List<Courier> couriers = courierDao.selectByPage(map);
        long total = courierDao.selectTotal(map);
        return R.suc(total, couriers);
    }

    @Override
    public R queryByDistrict(Map<String, Object> map) {
        List<Courier> couriers = courierDao.selectByDistrict(map);
        System.out.println(couriers);
        return R.suc(couriers);
    }

    @Override
    public R saveCourierAndPendingOrders(CourierAndPendingOrders courierAndPendingOrders) {
        int insert = courierAndPendingOrdersDao.insert(courierAndPendingOrders);
        return insert>0?R.suc():R.fail();
    }

    @Override
    public R queryByLogin(Courier courier) {
        courier.setPass(PasswordEncryption.getCiphertext(courier.getPass()));
        Courier courier1 = courierDao.selectByLogin(courier);
        return courier1==null?R.fail():R.suc(courier1);
    }
}
